
new Vue().$mount('#crumbs');
////////////--------------表格-数据--------------------////////////
var string="      <el-form :inline=\"true\">\n" +
    "                        <el-form-item style=\"margin-bottom: 0px;\" label=\"发送时间:\">\n" +
    "                            <el-date-picker  size=\"small\"  v-model=\"searchTime\"   type=\"datetimerange\"  placeholder=\"选择时间范围\" align=\"right\">\\n' +\n" +
    "                            </el-date-picker>\n" +
    "                        </el-form-item>\n" +
    "                        <el-form-item style=\"margin-bottom: 0px;\" label=\"\">\n" +
    "                            <el-input  v-model=\"searchValue\" size=\"small\" placeholder=\"请输入内容\"></el-input>\n" +
    "                        </el-form-item><el-form-item style=\"margin-bottom: 0px;\">\n" +
    "                        <el-button  size=\"small\" @change=\"_restore\" @keyup.enter.native=\"search\" @click=\"search\" type=\"default\">搜索</el-button>\n" +
    "                    </el-form-item>\n" +
    "                    </el-form>";
var tableFirstClick = 0;
var table;
var tableSecond;
var tableThird;

var tableTab = new Vue({
    el: "#tableTab",
    data: function() {
        return {
            activeName: 'first',
            unReadCount:0
        }
    },
    methods: {
        handleClick: function(tab, event) {
            //tab点击是否加载
            if(tab.index == tableFirstClick) {
                return;
            } else {
                tableFirstClick = tab.index;
            }
            if(tab.index==0)
            {
                if(table!=null||table!=undefined)
                {
                    table.getData({},true);
                    console.log("table");
                }

            }
            if(tab.index == 1) {
                if(tableSecond!=null||tableSecond!=undefined)
                {
                    tableSecond.getData({},true);
                    console.log("table2");
                }

            }
            if(tab.index == 2) {
                if(tableThird!=null||tableThird!=undefined)
                {
                    tableThird.getData({},true);
                    console.log("table3");
                }
            }
        },
        getUnReadCount:function () {
            VanyAjax("receivedMessageList/getUnReadMessageList",{},"GET",this.$el).done(function (data) {
                tableTab.unReadCount=data.total;
            },false)

        }
    }
})
tableTab.getUnReadCount();

tableSecond =getTableVue("tableSecond","receivedMessageList/getUnReadMessageList",[],"<div style='color:#0a6dcd;'>未读</div>",string);
Search(tableSecond);
toMsgDetail(tableSecond);
tableThird =getTableVue("tableThird","receivedMessageList/getReadMessageList",[],"<div style='color:#bebebe'>已读</div>",string);
Search(tableThird);
toMsgDetail(tableThird);
function Search(el) {
    el.on("search", function(searchValue,searchTime) {
        var startTime=searchTime===undefined?null:searchTime[0];
        var endTime=searchTime===undefined?null:searchTime[1];
        if(startTime!=null&&endTime!=null)
        {
            startTime=startTime.Format("yyyy-MM-dd HH:mm:ss");
            endTime=endTime.Format("yyyy-MM-dd HH:mm:ss");
        }
        el.getData({
            startTime:startTime,
            endTime:endTime,
            keyWord:searchValue
        }, true);
    });
};
table = getTableVue("table","receivedMessageList/getReceivedMessageList",
    [{
        authority: "received_delete",
        name: "批量删除",
        click:function () {
            deleteByids(this);
        }},
        {
            authority: "received_makeRead",
            name: "标记已读",
            type:"default",
            click:function () {
                makeAsRead(this);
            }},
        {
            authority: "received_makeAllRead",
            name: "全部已读",
            type:"default",
            click:function () {
                makeAsAllRead(this);
            }

        }
    ],'<div style="color:#bebebe"  v-if=\'scope.row.receiptStatus != null\'>已读</div><div v-else style=\'color:#0a6dcd;\'>未读</div>',string);
table.getData({},true);
Search(table);
toMsgDetail(table);
// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
//标记已读
function makeAsRead(el) {
    if(el.sels.length>0)
    {
            messageComfirm(el,"warning",'此操作将选择的所有消息标记为已读, 是否继续?', '提示',function(root){
                var ids=""
                var flag=true;
                for (var i=0;i<root.sels.length;i++)
                {
                    if(root.sels[i].receiptStatus!=null)
                    {
                        flag=false;
                    }
                    ids=ids+","+root.sels[i].msgId;
                }
                if(flag)
                {
                        VanyAjax("/receivedMessageList/makeAsReadByIds",{ids:ids},"POST",root.$el).done(function(data){
                        root.getData({},true);
                            tableTab.getUnReadCount();
                        },true,root);
                }else{
                    message(root, '消息已读，请重新选择！','warning');
                }
            });
    }else
    {
        message(el, '请至少选择一条消息！','warning');
    }
}

//全部已读
function makeAsAllRead(el)
{
        messageComfirm(el,"warning",'将全部未读消息设置为已读, 是否继续?', '提示',function(root){
            VanyAjax("/receivedMessageList/makeAsAllRead",{},"POST",root.$el).done(function(data){
                root.getData({},true);
                tableTab.getUnReadCount();
            },true,root);
        });
}
//批量删除
function deleteByids(el)
{
    if(el.sels.length>0)
    {
        messageComfirm(el,"warning",'此操作将删除选择的所有消息, 是否继续?', '提示',function(root){
            var ids=""
            for (var i=0;i<root.sels.length;i++)
            {
                ids=ids+","+root.sels[i].msgId;
            }
            VanyAjax("/receivedMessageList/deleteReceivedMessageByIds",{ids:ids},"POST",root.$el).done(function(data){
            root.getData({},true);
                tableTab.getUnReadCount();
            },true,root);
        });
    }else
    {
        message(el, '请至少选择一条消息！','warning');
    }
}
//详细页面跳转
function toMsgDetail(el)
{
    el.on("rowClick", function(row, event, column) {
       if(column.type!="selection")
       {
           localStorage.setItem("receivedMsgId",row.msgId);
           $("#content").load("templates/authority/receivedMessage/messageDetail.html");
           console.log(localStorage.getItem("receivedMsgId"));
       }
    });
}
//消息提示模板
function messageComfirm(el,type,msg,title,fn1)
{
    el.$confirm(msg, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: type
    }).then(function(){
       fn1(el);
    }).catch(function(){
        message(el,'已取消操作','info');
    })
}
function message(el,msg,type) {
    el.$message({
        message:msg,
        type:type
    });
}
//通用表格模板
function getTableVue(el,url,btns,template,zidingyiTop) {
   return  new VanyMyTable({
        el: "#"+el,
        url: url,
        index: false, //选框
        selection: true, //编号
        pageSize: 10, //默认每页10条
        //					pageSizes: [10, 20], //下拉分页
        pages: true, //是否需要分页，与scrollPage最好二选一
        scrollPage: false, //是否滚动分页，与pages最好二选一
        zidingyiTop:zidingyiTop,
        keys: [{
            "key": "msgtitle",
            "name": "消息标题"
        }, {
            "key": "senderName",
            "name": "发送人"
        }, {
            "key": "orgName",
            "name": "部门"
        }, {
            "key": "sendTime",
            "name": "发送时间"
        }, {
            "key": "receiptStatus",
            "name": "状态",
            "template": template
        }],
        // right、topRight、bottomLeft
        btnPostion: "bottomLeft",
        btns: btns
    });
    
}